package com.dev.common.quartz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author lsw
 * @data 2015-10-26
 * @version 1.0
 * @desc 数据备份
 */
public class DataBackJob {

	/**
	 * 日志记录
	 */
	private Logger log = LoggerFactory.getLogger(DataBackJob.class);

	/**
	 * 字符输出流
	 */
	private InputStream is;
	/**
	 * 字符输入流
	 */
	private OutputStream os;

	/**
	 * 数据库备份命令
	 */
	private String backCommand = "mysqldump -u root -p123456 chaffydish";

	/**
	 * 执行任务
	 */
	public void run() {
		log.info("备份数据库");
		if ("/".equals(File.separator)) {
//			this.DataBackByLinux();
		} else {
//			this.DataBackByWindow();
		}
	}

	/**
	 * window 下备份
	 */
	public void DataBackByWindow() {
		String mysqlPath = "C://Program Files (x86)//MySQL//MySQL Server 5.1//bin//";
		try {

			Process process = Runtime.getRuntime()
					.exec(mysqlPath + backCommand);
			is = process.getInputStream();
			os = new FileOutputStream("d://" + System.currentTimeMillis()
					+ ".sql");
			byte[] bs = new byte[1024];
			int length = 0;
			while ((length = is.read(bs)) != -1) {
				os.write(bs, 0, length);
			}

		} catch (IOException e) {
			log.error("数据备份失败！", e);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
				}
			}
		}
	}

	/**
	 * Linux 下备份
	 */
	public void DataBackByLinux() {
	}

}
